package com.heer.gymnasium.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.AppHelper;
import com.heer.gymnasium.R;
import com.heer.gymnasium.util.LoadImageUtil;
import com.heer.model.OrderBean;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by rzw2 on 2017/4/5.
 */

public class OrderSiteTypeAdapter extends ListAdapter<OrderBean.Booking> {
    private int count;
    private String image;

    public OrderSiteTypeAdapter(Activity activity, String image) {
        super(activity);
        this.image = image;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = mInflater.inflate(R.layout.item_order_list_item, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");

        Glide.with(mActivity)
                .load(LoadImageUtil.getLoadImageUrl(image))
                .placeholder(R.mipmap.ic_order_site_logo)
                .into(holder.imageSiteScene);
        final OrderBean.Booking item = getItem(position);
        holder.tvName.setText(item.getTitle());
        holder.tvTime.setText(sdf.format(new Date(Long.valueOf(item.getStime()) * 1000)) + "~" + sdf.format(new Date(Long.valueOf(item.getEtime()) * 1000)));

        if (item.getStatus()==null)
        {
            holder.tvCancle.setVisibility(View.GONE);
        }else if (item.getStatus().equals("1"))
        {
            holder.tvCancle.setVisibility(View.VISIBLE);
            holder.tvCancle.setText("取消");
            holder.tvCancle.setClickable(true);
        }else
        {
            holder.tvCancle.setVisibility(View.VISIBLE);
            holder.tvCancle.setText("已取消");
            holder.tvCancle.setClickable(false);
        }
        holder.tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setMessage("确认取消吗？");
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        AppHelper.context().apiService().orderBookCancle(item.getId()).enqueue(new BaseCallback<ApiResponse>() {
                            @Override
                            public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                                super.onResponse(call, response);
                                if (response.isSuccessful() && response.body().isSccessful()) {
                                    Toast.makeText(mActivity, "取消成功", Toast.LENGTH_SHORT).show();
                                    holder.tvCancle.setText("已取消");
                                    holder.tvCancle.setClickable(false);

                                } else {
                                    Toast.makeText(mActivity, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        });

        return view;
    }

    public void setCount(int count) {
        this.count = count;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return count;
    }

    static class ViewHolder {
        @BindView(R.id.image_site_scene)
        ImageView imageSiteScene;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_orderitem_cancle)
        TextView tvCancle;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
